<template>
	<view>
		<web-view :src="src" @load="bindload" @message="showMsg"></web-view>
		<!-- <web-view src="https://yludj.xetsl.com/s/1bCH1b" @load="bindload"></web-view> -->
	</view>
</template>

<script>
import { objectToRouteParams } from '@/common/util/util'
export default {
	data() {
		return {
			// src: 'https://bjielts-t.test.xdf.cn/listenClass?u2at='
			src: '',
			listenType: ''
		}
	},
	onLoad(option) {
		console.log(option)
		this.listenType = option.listenType || ''
		// let device = 'pc'
		// wx.getSystemInfo({
		//     success (res) {
		//         console.log(res.platform)
		//         if (res.platform == 'ios' || res.platform == 'android') {
		//             device = 'mobile'
		//         }
		//     }
		// })
		const distinctID = wx.sensors.getAnonymousID()
		const token = uni.getStorageSync('Access-Token')
		if(!token){
			let pages = getCurrentPages()
			const backRoute = pages.length && pages[pages.length - 1]
			if(Object.keys(backRoute)){
				const options = backRoute.options || {}
				const query = objectToRouteParams(options)
				const url = Object.keys(options).length ? `/${backRoute.route}?${query}` : `/${backRoute.route}`
				uni.setStorageSync('origin', url)
			}

			uni.showLoading({})
			setTimeout(function() {
				uni.redirectTo({
					url: '/pages/login/login?path=',
					complete() {
						console.log('完成跳转')
						uni.hideLoading()
					}
				});
			}, 200)
			return
		}
		if (process.env.NODE_ENV == 'development') {
			// this.src = 'https://bjielts-t.test.xdf.cn/listenClass?u2at='
			this.src = 'https://bjielts-t.test.xdf.cn/listen?u2at='
			// this.src = 'http://10.152.164.98:5173/listenClass?u2at='
			// this.src = 'http://10.152.164.121:5173/listenClass?u2at='
			// this.src = 'http://10.176.138.77:5173/listen?u2at='
		} else {
			// this.src = 'https://bjielts.xdf.cn/listenClass?u2at='
			this.src = 'https://bjielts.xdf.cn/listen?u2at='
		}
		this.src = `${
			this.src
		}${token}&t=${new Date().getTime()}&distinctID=${distinctID}&listenType=${
			option.listenType
		}`
		console.log(this.src)
	},
	onShow() {
		if (uni.getStorageSync('listenType')) {
			const distinctID = wx.sensors.getAnonymousID()
			const token = uni.getStorageSync('Access-Token')
			if(!token){
				let pages = getCurrentPages()
				const backRoute = pages.length && pages[pages.length - 1]
				if(Object.keys(backRoute)){
					const options = backRoute.options || {}
					const query = objectToRouteParams(options)
					const url = Object.keys(options).length ? `/${backRoute.route}?${query}` : `/${backRoute.route}`
					uni.setStorageSync('origin', url)
				}

				uni.showLoading({})
				setTimeout(function() {
					uni.redirectTo({
						url: '/pages/login/login?path=',
						complete() {
							console.log('完成跳转')
							uni.hideLoading()
						}
					});
				}, 200)
				return
			}
			const type = uni.getStorageSync('listenType') || this.listenType
			if (process.env.NODE_ENV == 'development') {
				// this.src = 'https://bjielts-t.test.xdf.cn/listenClass?u2at='
				this.src = 'https://bjielts-t.test.xdf.cn/listen?u2at='
				// this.src = 'http://10.152.164.98:5173/listenClass?u2at='
				// this.src = 'http://10.152.164.121:5173/listenClass?u2at='
				// this.src = 'http://10.176.138.77:5173/listen?u2at='
			} else {
				// this.src = 'https://bjielts.xdf.cn/listenClass?u2at='
				this.src = 'https://bjielts.xdf.cn/listen?u2at='
			}
			this.src = `${
				this.src
			}${token}&t=${new Date().getTime()}&distinctID=${distinctID}&listenType=${type}`
			uni.removeStorageSync('listenType')
			console.log(this.src)
		}
	},
	methods: {
		bindload() {},
		showMsg() {
			console.log('的弟弟')
		}
	}
}
</script>

<style scoped></style>
